package com.koushikdutta.ion;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Point;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.async.util.FileCache;
import com.koushikdutta.ion.bitmap.BitmapInfo;
import com.koushikdutta.ion.bitmap.IonBitmapCache;
import com.koushikdutta.ion.bitmap.PostProcess;
import com.koushikdutta.ion.bitmap.Transform;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;

class TransformBitmap extends BitmapCallback implements FutureCallback<BitmapInfo> {
    String downloadKey;
    ArrayList<PostProcess> postProcess;
    ArrayList<Transform> transforms;

    public static void getBitmapSnapshot(final Ion ion, final String transformKey, final ArrayList<PostProcess> postProcess) {
        if (ion.bitmapsPending.tag(transformKey) == null) {
            final BitmapCallback callback = new LoadBitmapBase(ion, transformKey, true);
            Ion.getBitmapLoadExecutorService().execute(new Runnable() {
                public void run() {
                    if (ion.bitmapsPending.tag(transformKey) == callback) {
                        try {
                            Bitmap bitmap = IonBitmapCache.loadBitmap(ion.responseCache.getFileCache().getFile(transformKey), null);
                            if (bitmap == null) {
                                throw new Exception("Bitmap failed to load");
                            }
                            Bitmap[] bitmapArr = new Bitmap[]{bitmap};
                            BitmapInfo info = new BitmapInfo(transformKey, "image/jpeg", bitmapArr, new Point(bitmap.getWidth(), bitmap.getHeight()));
                            info.loadedFrom = 1;
                            if (postProcess != null) {
                                Iterator i$ = postProcess.iterator();
                                while (i$.hasNext()) {
                                    ((PostProcess) i$.next()).postProcess(info);
                                }
                            }
                            callback.report(null, info);
                        } catch (OutOfMemoryError e) {
                            callback.report(new Exception(e), null);
                        } catch (Exception e2) {
                            callback.report(e2, null);
                            try {
                                ion.responseCache.getFileCache().remove(transformKey);
                            } catch (Exception e3) {
                            }
                        }
                    }
                }
            });
        }
    }

    public TransformBitmap(Ion ion, String transformKey, String downloadKey, ArrayList<Transform> transforms, ArrayList<PostProcess> postProcess) {
        super(ion, transformKey, true);
        this.transforms = transforms;
        this.downloadKey = downloadKey;
        this.postProcess = postProcess;
    }

    public void onCompleted(Exception e, final BitmapInfo result) {
        if (e != null) {
            report(e, null);
        } else if (this.ion.bitmapsPending.tag(this.key) == this) {
            Ion.getBitmapLoadExecutorService().execute(new Runnable() {
                public void run() {
                    OutOfMemoryError e;
                    Exception e2;
                    if (TransformBitmap.this.ion.bitmapsPending.tag(TransformBitmap.this.key) == TransformBitmap.this) {
                        Point size = null;
                        try {
                            Iterator i$;
                            Bitmap[] bitmaps = new Bitmap[result.bitmaps.length];
                            int i = 0;
                            while (i < result.bitmaps.length) {
                                bitmaps[i] = result.bitmaps[i];
                                i$ = TransformBitmap.this.transforms.iterator();
                                Point size2 = size;
                                while (i$.hasNext()) {
                                    try {
                                        Bitmap bitmap = ((Transform) i$.next()).transform(bitmaps[i]);
                                        if (bitmap == null) {
                                            throw new Exception("failed to transform bitmap");
                                        }
                                        bitmaps[i] = bitmap;
                                        if (size2 == null) {
                                            size = new Point(bitmap.getWidth(), bitmap.getHeight());
                                        } else {
                                            size = size2;
                                        }
                                        size2 = size;
                                    } catch (OutOfMemoryError e3) {
                                        e = e3;
                                        size = size2;
                                    } catch (Exception e4) {
                                        e2 = e4;
                                        size = size2;
                                    }
                                }
                                i++;
                                size = size2;
                            }
                            BitmapInfo info = new BitmapInfo(TransformBitmap.this.key, result.mimeType, bitmaps, size);
                            info.delays = result.delays;
                            info.loadedFrom = result.loadedFrom;
                            if (TransformBitmap.this.postProcess != null) {
                                i$ = TransformBitmap.this.postProcess.iterator();
                                while (i$.hasNext()) {
                                    ((PostProcess) i$.next()).postProcess(info);
                                }
                            }
                            TransformBitmap.this.report(null, info);
                            if (info.bitmaps.length <= 1) {
                                FileCache cache = TransformBitmap.this.ion.responseCache.getFileCache();
                                if (cache != null) {
                                    File tempFile = cache.getTempFile();
                                    try {
                                        FileOutputStream out = new FileOutputStream(tempFile);
                                        info.bitmaps[0].compress(info.bitmaps[0].hasAlpha() ? CompressFormat.PNG : CompressFormat.JPEG, 100, out);
                                        out.close();
                                        cache.commitTempFiles(TransformBitmap.this.key, tempFile);
                                    } catch (Exception e5) {
                                    } finally {
                                        tempFile.delete();
                                    }
                                }
                            }
                        } catch (OutOfMemoryError e6) {
                            e = e6;
                            TransformBitmap.this.report(new Exception(e), null);
                        } catch (Exception e7) {
                            e2 = e7;
                            TransformBitmap.this.report(e2, null);
                        }
                    }
                }
            });
        }
    }
}
